package com.usefullc.download.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.common.exception.BizException;
import com.usefullc.download.param.CheckRegisterCodeParam;
import com.usefullc.download.param.RegisterCodeGenParam;
import com.usefullc.download.service.RegisterCodeService;
import com.usefullc.download.vo.CheckRegisterCodeVo;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: Sampson
 * @date: 2022-01-10 15:28
 */
@RestController
@RequestMapping(path = "/download/settings/")
@Api( tags="设置管理")
public class SettingsController extends BaseController {

    @Autowired
    private RegisterCodeService registerCodeService;


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "检测注册码", response = WebResult.class)
    @PostMapping(path = "checkRegisterCode", produces = "application/json")
    public WebResult checkRegisterCode(@RequestBody CheckRegisterCodeParam param) {
        if (StringUtils.isEmpty(param.getMachineId())) {
            throw new BizException("机器码不能为空");
        }
        CheckRegisterCodeVo checkRegisterCodeVo = registerCodeService.checkRegisterCode(param);
        return WebResultBuilder.successResult(checkRegisterCodeVo);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "激活注册码", response = WebResult.class)
    @PostMapping(path = "activeRegisterCode", produces = "application/json")
    public WebResult activeRegisterCode(@RequestBody CheckRegisterCodeParam param) {
        if (StringUtils.isEmpty(param.getMachineId())) {
            throw new BizException("机器码不能为空");
        }
        if (StringUtils.isEmpty(param.getRegisterCode())) {
            throw new BizException("注册码不能为空");
        }
        CheckRegisterCodeVo checkRegisterCodeVo = registerCodeService.activeRegisterCode(param);
        return WebResultBuilder.successResult("激活成功",checkRegisterCodeVo);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "生成注册码", response = WebResult.class)
    @PostMapping(path = "genCode", produces = "application/json")
    public WebResult genCode(@RequestBody RegisterCodeGenParam param) {
        if (param.getCodeNum() == null) {
            throw new BizException("注册码数量不能为空");
        }
        if (param.getDayCount() == null) {
            throw new BizException("注册码天数不能为空");
        }

        registerCodeService.genCode(param);
        return WebResultBuilder.successResult();
    }

}
